package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.mass.request.MassCancelRequest;
import com.fulihui.wgj.admin.api.mass.request.MassLogPageQueryRequest;
import com.fulihui.wgj.admin.api.mass.request.SendMassRequest;
import com.fulihui.wgj.admin.api.mass.vo.MassMsgLogVO;
import com.fulihui.wgj.admin.config.properties.AdminProperties;
import com.fulihui.wgj.admin.integration.SendWxMassServiceClient;
import com.fulihui.wgj.facade.dto.WxMassMsgLogDTO;
import com.fulihui.wgj.facade.request.mass.CalcMassMsgRequest;
import com.fulihui.wgj.facade.request.mass.WxMassLogCancelRequest;
import com.fulihui.wgj.facade.request.mass.WxMassLogPageQueryRequest;
import com.fulihui.wgj.facade.result.mass.WxMassLogPageResult;
import com.fulihui.wgj.facade.service.mass.SendWxMassService;
import com.fulihui.wgj.facade.service.mass.WxMassLogService;
import com.fulihui.wgj.facade.service.template.SendTemplateMsgService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.List;

/**
 * @author: Levon
 * @date: 2018-08-02 09:32
 */
@Service
public class SendWxMassServiceClientImpl implements SendWxMassServiceClient {

    @Reference(version = "1.0.0")
    private SendWxMassService sendWxMassService;

    @Reference(version = "1.0.0")
    private WxMassLogService wxMassLogService;

    @Reference(version = "1.0.0")
    private SendTemplateMsgService sendTemplateMsgService;

    @Autowired
    private AdminProperties adminProperties;

    @Override
    public Result<Boolean> sendMass(SendMassRequest request) {
        BaseResult baseResult = sendWxMassService.sendMassMsg(buildRequest(request));
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<String> countUser(SendMassRequest request) {
        SimpleResult<Long> simpleResult = sendTemplateMsgService.countSendUser(buildRequest(request));
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return JsonResultBuilder.succ(String.valueOf(simpleResult.getData()));
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<Boolean> cancelMass(MassCancelRequest request) {
        WxMassLogCancelRequest cancelRequest = new WxMassLogCancelRequest();
        cancelRequest.setId(request.getId());
        BaseResult baseResult = wxMassLogService.cancelMassLog(cancelRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public PageView<MassMsgLogVO> massLog(MassLogPageQueryRequest request) {

        WxMassLogPageQueryRequest queryRequest = new WxMassLogPageQueryRequest();
        queryRequest.setAppId(request.getAppId());
        queryRequest.setTitle(request.getRemark());
        queryRequest.setBeginDate(request.getBeginDate());
        queryRequest.setEndDate(request.getEndDate());
        queryRequest.setSendStatus(request.getSendStatus());
        queryRequest.setPage(request.getPage());
        queryRequest.setRows(request.getRows());

        List<MassMsgLogVO> massMsgLogVOS = Lists.newArrayList();

        WxMassLogPageResult pageResult = wxMassLogService.pageQuerySendLogs(queryRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<WxMassMsgLogDTO> massMsgLogDTOS = pageResult.getValue();
            if (!CollectionUtils.isEmpty(massMsgLogDTOS)) {
                for (WxMassMsgLogDTO massMsgLogDTO : massMsgLogDTOS) {
                    MassMsgLogVO massMsgLogVO = BeanConv.copy(massMsgLogDTO, MassMsgLogVO.class);
                    massMsgLogVO.setGmtCreate(DateUtils.formatNewFormat(massMsgLogDTO.getGmtCreate()));
                    massMsgLogVO.setHeadPicUrl(adminProperties.getImgproxy() + massMsgLogDTO.getHeadPicUrl());
                    massMsgLogVO.setGmtModified(DateUtils.formatNewFormat(massMsgLogDTO.getGmtModified()));
                    massMsgLogVO.setRemark(massMsgLogDTO.getTitle());
                    massMsgLogVOS.add(massMsgLogVO);
                }
            }
        }
        return PageViewBuilder.build(massMsgLogVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }


    private CalcMassMsgRequest buildRequest(SendMassRequest request) {
        CalcMassMsgRequest massMsgRequest = new CalcMassMsgRequest();
        massMsgRequest.setMediaId(request.getMediaId());
        massMsgRequest.setTitle(request.getTitle());
        massMsgRequest.setAppId(request.getAppId());
        massMsgRequest.setProvince(request.getProvince());
        massMsgRequest.setProvinceTab(request.getProvinceTab());
        String sex = request.getSex();
        if (StringUtils.isNotBlank(sex)) {
            massMsgRequest.setSex(Long.valueOf(request.getSex()));
        }
        massMsgRequest.setSexTab(request.getSexTab());
        massMsgRequest.setQrSceneStr(request.getSceneStr());
        massMsgRequest.setQrSceneStrTab(request.getSceneStrTag());
        massMsgRequest.setCronTab(request.isCronTab());
        try {
            massMsgRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            massMsgRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
            massMsgRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        massMsgRequest.setCountry(request.getCountry());
        massMsgRequest.setCountryTab(request.getCountryTab());
        massMsgRequest.setSendConditionDesc(request.getSendConditionDesc());
        return massMsgRequest;
    }
}
